package com.mycompany.myapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.Yong;
import com.mycompany.myapp.service.YongService;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import com.mycompany.myapp.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Yong.
 */
@RestController
@RequestMapping("/api")
public class YongResource {

    private final Logger log = LoggerFactory.getLogger(YongResource.class);

    private static final String ENTITY_NAME = "yong";

    private final YongService yongService;

    public YongResource(YongService yongService) {
        this.yongService = yongService;
    }

    /**
     * POST  /yongs : Create a new yong.
     *
     * @param yong the yong to create
     * @return the ResponseEntity with status 201 (Created) and with body the new yong, or with status 400 (Bad Request) if the yong has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/yongs")
    @Timed
    public ResponseEntity<Yong> createYong(@RequestBody Yong yong) throws URISyntaxException {
        log.debug("REST request to save Yong : {}", yong);
        if (yong.getId() != null) {
            throw new BadRequestAlertException("A new yong cannot already have an ID", ENTITY_NAME, "idexists");
        }
        Yong result = yongService.save(yong);
        return ResponseEntity.created(new URI("/api/yongs/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /yongs : Updates an existing yong.
     *
     * @param yong the yong to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated yong,
     * or with status 400 (Bad Request) if the yong is not valid,
     * or with status 500 (Internal Server Error) if the yong couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/yongs")
    @Timed
    public ResponseEntity<Yong> updateYong(@RequestBody Yong yong) throws URISyntaxException {
        log.debug("REST request to update Yong : {}", yong);
        if (yong.getId() == null) {
            return createYong(yong);
        }
        Yong result = yongService.save(yong);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, yong.getId().toString()))
            .body(result);
    }

    /**
     * GET  /yongs : get all the yongs.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of yongs in body
     */
    @GetMapping("/yongs")
    @Timed
    public ResponseEntity<List<Yong>> getAllYongs(Pageable pageable) {
        log.debug("REST request to get a page of Yongs");
        Page<Yong> page = yongService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/yongs");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /yongs/:id : get the "id" yong.
     *
     * @param id the id of the yong to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the yong, or with status 404 (Not Found)
     */
    @GetMapping("/yongs/{id}")
    @Timed
    public ResponseEntity<Yong> getYong(@PathVariable Long id) {
        log.debug("REST request to get Yong : {}", id);
        Yong yong = yongService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(yong));
    }

    /**
     * DELETE  /yongs/:id : delete the "id" yong.
     *
     * @param id the id of the yong to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/yongs/{id}")
    @Timed
    public ResponseEntity<Void> deleteYong(@PathVariable Long id) {
        log.debug("REST request to delete Yong : {}", id);
        yongService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
